<template>
  <div class="address-tab">
    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="consignee" label="收货人" width="180"></el-table-column>
      <el-table-column prop="addressInfo" label="所在地" width="300"></el-table-column>
      <el-table-column prop="zipCode" label="邮编"></el-table-column>
      <el-table-column prop="phone" label="手机号"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="updateAddress(scope.row)">修改</el-button>
          <el-button type="text" size="small" @click="del(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="设为默认">
        <template slot-scope="scope">
          <el-button v-if="scope.row.isDefault == 1" type="danger" size="small" disabled>默认地址</el-button>
          <el-button v-else type="text" @click="setDefault(scope.row.id)">设为默认</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  data() {
    return {};
  },
  computed: {
    tableData() {
      return this.adressList.map(item => {
        return {
          id: item.id,
          consignee: item.consignee,
          addressInfo: `${item.province}-${item.city}-${item.region}-${item.street}`,
          zipCode: item.zipCode,
          phone: item.phone,
          isDefault: item.isDefault,
          detailed: item.detailed
        };
      });
    }
  },
  props: {
    adressList: Array
  },
  methods: {
    del(id) {
      this.$confirm("是否确定删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$emit("delAddress", id);
        })
        .catch(() => {});
    },
    setDefault(id) {
      this.$confirm("是否设为默认地址?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$emit("setDefault", id);
        })
        .catch(() => {});
    },
    updateAddress(column) {
      console.log(column);
      this.$confirm("是否更新", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$emit("updateAddress", column);
        })
        .catch(() => {});
    }
  }
};
</script>
<style lang="less" scoped>
  .address-tab {
    padding-bottom: 30px;
  }
</style>